/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Service;

import DB.Bitacora;
import DB.HibernateUtil;
import DB.Usuario;
import com.google.gson.Gson;
import java.util.Calendar;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Rodrigo
 */
@WebService()
public class BitacoraWS {

    Gson gson2 = new Gson(); // utilizado solo para poder similar una session de usuario en el sistema

    /**
     * Web service operation
     */
    @WebMethod(operationName = "listarBitacora")
    public List<Bitacora> listarBitacora() {
        //TODO write your implementation code here:
        return ControladorDeObjetos.getAllObjectByClassType(Bitacora.class);
    }

    /**
     * Web service operation que filtra por rut, nombre, modulo, accion, valor nuevo, valor anterior, fecha
     */
    @WebMethod(operationName = "buscarBitacora")
    public String buscarBitacora(@WebParam(name = "buscar") String buscar) {
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction trans = s.beginTransaction();
        Criteria c = s.createCriteria(Bitacora.class).add(Restrictions.or(Restrictions.or(Restrictions.or(Restrictions.ilike("rut", "%" + buscar + "%"), Restrictions.ilike("nombre", "%" + buscar + "%")),
                Restrictions.or(Restrictions.ilike("modulo", "%" + buscar + "%"), Restrictions.ilike("accion", "%" + buscar + "%"))),Restrictions.or(Restrictions.ilike("fecha", "%" + buscar + "%"), Restrictions.ilike("hora", "%" + buscar + "%"))));
        List l = c.list();
        Gson gson = new Gson();
        String json = gson.toJson(l);
        s.close();
        return json;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "registrar")
    public Long registrar(@WebParam(name = "usuario") Usuario usuario, @WebParam(name = "modulo") String modulo, @WebParam(name = "accion") String accion, @WebParam(name = "valorAnterior") String valorAnterior, @WebParam(name = "valorNuevo") String valorNuevo) {
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = s.beginTransaction();
        Bitacora b = new Bitacora();
        //obtener rut de lgin
        b.setRut(usuario.getRut());
        //obtener Nombre a traves del rut
        b.setNombre(usuario.getNombres()+" "+usuario.getApellidoPaterno()+" "+usuario.getApellidoMaterno());
        b.setModulo(modulo);
        b.setAccion(accion);
        b.setValorAnterior(valorAnterior);
        b.setValorNuevo(valorNuevo);
        Calendar c = Calendar.getInstance();
        String dia = Integer.toString(c.get(Calendar.DAY_OF_MONTH));
        if (dia.length() == 1) {
            dia = "0" + Integer.toString(c.get(Calendar.DAY_OF_MONTH));
        }
        String mes = Integer.toString(c.get(Calendar.MONTH) + 1);
        if (mes.length() == 1) {
            mes = "0" + Integer.toString(c.get(Calendar.MONTH));
        }
        if(mes.equals("00")){
            mes="01";
        }
        String fecha = dia + "-" + mes + "-"
                + Integer.toString(c.get(Calendar.YEAR));
        b.setFecha(fecha);
        b.setHora(c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE));
        s.save(b);
        t.commit();
        return b.getId();
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "registrarFalso")
    public Long registrarFalso(@WebParam(name = "modulo") String modulo, @WebParam(name = "accion") String accion, @WebParam(name = "valorAnterior") String valorAnterior, @WebParam(name = "valorNuevo") String valorNuevo) {
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction t = s.beginTransaction();
        Bitacora b = new Bitacora();
        //obtener rut de lgin
        // b.setRut(usuario.getRut());
        b.setRut("17.522.583-8");
        //obtener Nombre a traves del rut
        // b.setNombre(usuario.getNombres());
        b.setNombre("Rodrigo Alejandro");
        b.setModulo(modulo);
        b.setAccion(accion);
        b.setValorAnterior(valorAnterior);
        b.setValorNuevo(valorNuevo);
        Calendar c = Calendar.getInstance();
        String dia = Integer.toString(c.get(Calendar.DAY_OF_MONTH));
        if (dia.length() == 1) {
            dia = "0" + Integer.toString(c.get(Calendar.DAY_OF_MONTH));
        }
        String mes = Integer.toString(c.get(Calendar.MONTH) + 1);
        if (mes.length() == 1) {
            mes = "0" + Integer.toString(c.get(Calendar.MONTH));
        }
        String fecha = dia + "-" + mes + "-"
                + Integer.toString(c.get(Calendar.YEAR));
        b.setFecha(fecha);//c.get(Calendar.DAY_OF_MONTH) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.YEAR)
        b.setHora(c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE));
        s.save(b);
        t.commit();
        return b.getId();
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "cargarBitacora")
    public Object cargarBitacora(@WebParam(name = "id") Long id) {
        //TODO write your implementation code here:
        return ControladorDeObjetos.loadObjectByLongId(id, Bitacora.class);
    }
}
